{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bf1579bc-03b0-4703-8157-6525a7c3b84f",
   "metadata": {},
   "source": [
    "# LCEL: LangChain表达式语言"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4be5ed6a-0a28-4aff-af25-8cfb14bf53d6",
   "metadata": {},
   "source": [
    "## LCEL的Pipeline：\n",
    "![Alt Text](lcel01.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b7c8081-70c1-41c0-9931-378df2127810",
   "metadata": {},
   "source": [
    "## 一个最简单示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1b03def-1c6d-4356-a5f4-8884f9f53b63",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "prompt = ChatPromptTemplate.from_template(\"给我讲一个关于 {topic}的笑话\")\n",
    "model = ChatOpenAI(model=\"gpt-4\")\n",
    "output_parser = StrOutputParser()\n",
    "\n",
    "chain = prompt | model | output_parser\n",
    "\n",
    "chain.invoke({\"topic\": \"冰激凌\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69f40555-e89e-4140-9ce1-cdfb23cf9d37",
   "metadata": {},
   "source": [
    "## RAG Search Exampl\n",
    "- 建立向量数据\n",
    "- 使用RAG增强"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abd37bcb-0cc8-4388-89e1-c9ca4e7e1c93",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install --upgrade --quiet  langchain langchain-openai faiss-cpu tiktoken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da6bc7c3-b911-4776-8499-0b1acb244ee4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from operator import itemgetter\n",
    "\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.runnables import RunnableLambda, RunnablePassthrough\n",
    "from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
    "\n",
    "vectorstore = FAISS.from_texts(\n",
    "    [\"harrison worked at kensho\"], embedding=OpenAIEmbeddings()\n",
    ")\n",
    "retriever = vectorstore.as_retriever()\n",
    "\n",
    "template = \"\"\"Answer the question based only on the following context:\n",
    "{context}\n",
    "\n",
    "Question: {question}\n",
    "\"\"\"\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "model = ChatOpenAI()\n",
    "\n",
    "chain = (\n",
    "    {\"context\": retriever, \"question\": RunnablePassthrough()}\n",
    "    | prompt\n",
    "    | model\n",
    "    | StrOutputParser()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "673719e3-5ff5-4160-af6c-227edc8bac0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain.invoke(\"where did harrison work?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebce3859-43e6-46ef-8c28-b75aa372c84c",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"Answer the question based only on the following context:\n",
    "{context}\n",
    "\n",
    "Question: {question}\n",
    "\n",
    "Answer in the following language: {language}\n",
    "\"\"\"\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "chain = (\n",
    "    {\n",
    "        \"context\": itemgetter(\"question\") | retriever,\n",
    "        \"question\": itemgetter(\"question\"),\n",
    "        \"language\": itemgetter(\"language\"),\n",
    "    }\n",
    "    | prompt\n",
    "    | model\n",
    "    | StrOutputParser()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ba7dde3-224c-4f0a-91bb-5e12d278e9fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain.invoke({\"question\": \"where did harrison work\", \"language\": \"chinese\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (langchain)",
   "language": "python",
   "name": "langchain-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
